package com.letoy.edu.service.Impl;

import com.letoy.edu.dao.LessonArrMapper;
import com.letoy.edu.dao.ScoreInfoMapper;
import com.letoy.edu.entity.LessonArr;
import com.letoy.edu.entity.ScoreInfo;
import com.letoy.edu.service.LessonArrService;
import com.letoy.edu.vo.SelectLesson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class LessonArrServiceImpl implements LessonArrService {


    @Autowired
    LessonArrMapper lessonArrMapper;

    @Autowired
    ScoreInfoMapper scoreInfoMapper;

    @Override
    public HashMap<String, Object> findAllLessonArr() {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<LessonArr> allLessonArr = lessonArrMapper.findAllLessonArr();
        if (allLessonArr.isEmpty()) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", allLessonArr);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> findLessonArrByNumberWeek(int number, int week) {
        HashMap<String, Object> hashMap = new HashMap<>();

        List<SelectLesson> lessonArrByNumberWeek = lessonArrMapper.findLessonArrByNumberWeek(number, week);

        if (lessonArrByNumberWeek.isEmpty()) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", lessonArrByNumberWeek);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> insertLessonArr(int studentId, int leesonId) {

        HashMap<String, Object> hashMap = new HashMap<>();
        ScoreInfo scoreInfo = new ScoreInfo();
        scoreInfo.setLessonId(leesonId);
        scoreInfo.setStudentId(studentId);
        int scoreResult = scoreInfoMapper.insertScoreInfo(scoreInfo);
        if (scoreResult == 0) {
            hashMap.put("status", "1");
            hashMap.put("msg", "插入失败");
            return hashMap;
        }
        // 我需要知道scoreId
//        System.out.println(scoreInfo.getScoreId());
        LessonArr lessonArr = new LessonArr();
        lessonArr.setStudentId(studentId);
        lessonArr.setLessonId(leesonId);
        lessonArr.setScoreId(scoreInfo.getScoreId());
        int result = lessonArrMapper.insertLessonArr(lessonArr);
        if (result == 0) {
            hashMap.put("status", "1");
            hashMap.put("msg", "插入失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "插入成功");
        return hashMap;
    }
}
